package com.bao.system.controller;


import com.bao.response.Result;
import com.bao.system.entity.Entity;
import com.bao.system.service.EntityService;
import com.bao.vo.EntityVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-06-09
 */
@RestController
@RequestMapping("/system/entity")
//@CrossOrigin
public class EntityController {

    @Autowired
    EntityService entityService;

    @PostMapping("/findEntityPage")
    @ApiOperation(value = "查询所有资源信息",notes = "查询资源用户")
    public Result findEntityPage(@RequestParam(required = true, defaultValue = "1") Integer currentPage,
                               @RequestParam(required = true, defaultValue = "7") Integer pageSize,
                               @RequestBody EntityVO EntityVO){
        System.out.println("------------------------");
        System.out.println(EntityVO);
        System.out.println(currentPage +"-"+ pageSize);
        Page<Entity> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Entity> wrapper = getWrapper(EntityVO);
        IPage<Entity> entityPage = entityService.findEntityPage(page, wrapper);
        long total = entityPage.getTotal();
        List<Entity> EntityList = entityPage.getRecords();
        System.out.println(EntityList);
        return Result.ok().data("total",total).data("entityList",EntityList);
    }

    private QueryWrapper<Entity> getWrapper(EntityVO entityVO){
        QueryWrapper<Entity> wrapper = new QueryWrapper<>();
        if (entityVO != null) {
            if(!StringUtils.isEmpty(entityVO.getEntityname())){
                wrapper.eq("entityname",entityVO.getEntityname());
            }
            if(!StringUtils.isEmpty(entityVO.getStatus())){
                wrapper.eq("status",entityVO.getStatus());
            }
        }
        return wrapper;
    }

    @GetMapping("/deleEntity")
    @ApiOperation(value = "删除资源",notes = "根据id删除资源")
    public Result deleEntity(@RequestParam("id") Integer id){
        System.out.println("------------------------");
        entityService.deleEntity(id);
        return Result.ok();
    }

    @GetMapping("/examineEntity")
    @ApiOperation(value = "审核通过",notes = "资源审核通过")
    public Result examineEntity(@RequestParam("id") Integer id){
        entityService.examineEntity(id);
        return Result.ok();
    }

    @GetMapping("/addEntity")
    @ApiOperation(value = "审核通过",notes = "资源审核通过")
    public Result addEntity(Entity entity){
        System.out.println(entity);
        entity.setCreattime(new Date());
        System.out.println(entity);
        entityService.addEntity(entity);
        return Result.ok();
    }
}

